Thanks to John Norstad and Ephraim Vishniac for help and comments. Portions of this code are based on [Morton 90], which appears in the May 1990 issue of MacTutor. Reused with permission. You may copy, alter, use, and distribute all code listed here if you leave the file unchanged up to this line.
Think C version.
Notes:
• A main advantage of this code, as explained in the “Methods and History” section, is that its effectiveness is not diminished by its availability. No matter how many potential virus authors read it, the algorithm will remain equally effective against circumvention.
• To use this code in your programs:
— it will be necessary to obtain non-functional
but significant (larger than 300 bytes) resource segments from the virus you are trying to detect
- using a resource editor, insert the viral data under an unused type, such as 'VDAT', used in the code below
- this will render the virus code inactive and most likely invisible to conventional (Class 2 and 3) detection programs; as and added security measure, you may wish to include only code segments above 300 bytes (or a similar threshold length) to ensure that the virus is crippled
- both these C routines and the Boyer-Moore routines require an expanded 512K Mac or later (specifically, System 3.2 or later); they have been fully tested on the SE, II, and IIcx
- this function should be run upon first launching your application, or, if it is an operating system utility, during a “dormant” or idle period
- the code below assumes that the VDAT resource contains all 5 segments of nVIR A; change this accordingly by adding additional virus types (under a name other than the original infected type) */
#include "dec.h"
FILE *my_file;
void dynamic()
{
char m[MAXSIZE];
int pattern_length, index;
MATRIX table;
register Handle rsrc;
short resCount;
ToolBoxInit();
CurResFile();
resCount = Count1Resources ('VDAT');
/* how many of this type are there? */
open_file (&my_file, WRITE_MODE);
#ifdef _REPORT /* developer debug flag */
printf("Searching for <<virus name>>:\n\n");
fprintf(my_file, "Searching for <<virus name>>:\n\n");
#endif
while (resCount) /* loop down to 1 */
{
if (resCount == 3)
{
printf("\nSearching for <<virus name>>:\n\n");
fprintf(my_file, "\nSearching for <<virus name>>:\n\n");
}
rsrc = Get1IndResource ('VDAT', resCount);
/* get the resource's handle, but don't load it */
index = SizeResource (rsrc);
HLock (rsrc);
/* load next virus segment */
pattern_length = copy_array (*rsrc, m, &index);
#ifdef _REPORT
printf("Next virus segment loaded (length %d). Resources left to scan: %d\n", pattern_length, resCount);
fprintf(my_file, "Next virus segment loaded (length %d). Resources left to scan: %d\n", pattern_length, resCount);
#endif
HUnlock (rsrc);
initialize(&table, pattern_length+1);
vResCheck('nVRB', m, pattern_length, table, NO_REPORT);
# ifdef _REPORT
printf("\n");
fprintf(my_file, "\n");
# endif
vResCheck('nVRA', m, pattern_length, table, NO_REPORT);